<?php
/**
 * 汽车品牌
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/19
 * Time: 下午6:42
 */
namespace app\common\model;

use think\Model;

class CarBrandModel extends Model
{
    const SHOW_STATUS=1; //正常
    const HIDE_STATUS=0; //禁用
    const DEL_STATUS=-1; //删除

    /**
     * 所有品牌
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where=[],$pageSum=0)
    {
        if($pageSum<=0){
            $pageSum=getSystem("pageSUM")?getSystem("pageSUM"):10;
        }

        $list=self::where('status','>',self::DEL_STATUS)
            ->where($where)
            ->order('hot desc,shot desc,brand_first asc')
            ->paginate($pageSum);

        return $list;
    }

    /**
     * 获取所有数据
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($where=[]){
        $list=self::cache()
            ->field("brand_first,brand,brand_id,brand_logo")
            ->where('status',self::SHOW_STATUS)
            ->where($where)
            ->order('brand_first asc')
            ->select();

        return $list;
    }


    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id){

        $au=self::get($id);
        $au->status=self::DEL_STATUS;

        $status=$au->save();
        if($status){
            return ['status'=>1,'msg'=>'删除成功！'];
        }else{
            return ['status'=>0,'msg'=>'删除失败！'];
        }
    }

    /**
     * 增加
     * @param $data
     * @return array
     * @throws \think\exception\DbException
     */
    public function adds($data){

        if(empty($data['id'])){
            $status = self::save($data);

            if($status){
                return['status'=>1,'msg'=>'增加成功！'];
            }else{
                return['status'=>0,'msg'=>'增加失败！'];
            }
        }else{
            $status = self::save($data,['id'=>$data["id"]]);

            if($status){
                return['status'=>1,'msg'=>'修改成功！'];
            }else{
                return['status'=>0,'msg'=>'修改失败！'];
            }
        }

    }

} 